var _Scene = require('component/scene.js');
var _layout = require('tools/layout.js');
var _relative = _layout.relative;

var _global = require('global/global.js');
var R = require('res/texture.js').R;
var S = require('res/style.js');

var _UIUtils = require('scenes/game/utils.js');
var _Constant = require('scenes/game/constant.js');

var _Widgets = require('./confirm/title.js');
var _Settings = require('scenes/constants.js');
var _BackListener = require('global/backlistener.js');
//var GC = require('nativeclasses').gcobserver;

// ==========================
// Confirm
// ==========================
var Confirm = _Scene.createScene(
    function (game) {
        var screen = _global.screen;
        var w = screen.width(), h = screen.height();
        this.setId('confirm');
        this.mGame = game;

        this.addChild(this.mBg = _global.colorNode('ffffff', w, h));
        this.addChild(this.mTitle = _Widgets.createTitle('Are you sure?'));
        this.addChild(this.mMessage = _Widgets.createMessage('Careful!\nYou will lost all progress\non your current board.'));
        this.addChild(this.mEndGame = _UIUtils.createLongButton(R.ui.bt_confirm_up, R.ui.bt_confirm_down, 'end', 'END GAME'));
        this.addChild(this.mKeepPlaying = _UIUtils.createLongButton(R.ui.bt_play_up, R.ui.bt_play_down, 'cancel', 'KEEP PLAYING'));

        this.querySelector('button').forEach(function (button) {
            button.on('click', this.onButtonClick, this);
        }, this);

        this.onSizeChange(w, h);
    }
);
Confirm.prototype.onSizeChange = function (w, h) {
    this.setSize(w, h);
    this.mBg.setSize(w, h);

    _relative.layout(this.mTitle, 0.5, 1, w * 0.5, h);
    _relative.layoutTo(this.mMessage, 0.5, 1, this.mTitle, 0.5, 0);

    var bottom = _Settings.adsHeight;
    var top = h - this.mTitle.height() - this.mMessage.height();

    var b1 = bottom + (top - bottom) * 0.7;
    var b2 = bottom + (b1 - bottom - 20) * 0.5;
    _relative.layout(this.mEndGame, 0.5, 0.5, w * 0.5, b1);
    _relative.layout(this.mKeepPlaying, 0.5, 0.5, w * 0.5, b2);
};
// ----------- event process
Confirm.prototype.onButtonClick = function (button) {
    _global.cancelEvents();
    switch (button.getId()) {
        case 'end':
            this.endGame();
            break;
        case 'cancel':
            this.turnsDown();
            break;
    }
};

Confirm.prototype.onEnter = function () {
    _BackListener.bindScene(this);
};
Confirm.prototype.turnsUp = function () {
    require('scenes/widget/sceneanima.js')
        .createAnima(this.mGame, this)
        .direction('right')
        .gcOnStart(false)
        .setMode(true, true)
        .start();
};
Confirm.prototype.turnsDown = function () {
    require('scenes/widget/sceneanima.js')
        .createAnima(this.mGame, this)
        .direction('right')
        .gcOnStart(false)
        .setMode(false, true)
        .start();
    this.mGame = null;
};
Confirm.prototype.endGame = function () {
    this.mGame.confirmExit();
    this.mGame = null;
    require('scenes/cover.js').newInstance().showCover(this);
};

Confirm.prototype.recoverFromLostContext = function (context) {
    _Scene.prototype.recoverFromLostContext.call(this, context);
    var game = this.mGame;
    if (game) {
        game.recoverFromLostContext(context);
    }
};

module.exports = Confirm;